//
//  TableViewController.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/10.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import CooperSwift
import RxSwift

struct TabModel {
    var title: String
    var image: UIImage?
    var selecedIMG: UIImage?
    
    static func tab(_ title: String = "", normalImage: UIImage?, selectedImage: UIImage?) -> TabModel {
        return TabModel(title: title, image: normalImage?.withRenderingMode(.alwaysOriginal), selecedIMG: selectedImage?.withRenderingMode(.alwaysOriginal))
    }
}

class TabbarC: UITabBarController, SelfAware {
    
    static func awake() {
        debugPrint("awake")
    }

    var tabModels: [TabModel]?
    
    var childVCs: [UIViewController]?
    
    let bag = DisposeBag()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    convenience init(tabModels tabItems: [TabModel], childVCs vcs: [UIViewController]) {
        self.init()
        self.tabModels = tabItems
        self.childVCs = vcs
        reloadConfig()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        sh_prefersNavigationBarHidden = true
        
        countdownTimer(second: 4) { index in
            debugPrint("index --> \(index)")
        }.subscribe { () in
            
        } onFailure: { error in
            debugPrint(error)
        }.disposed(by: bag)

        
    }
    
    func reloadConfig() {
        guard let _ = tabModels else {
            return
        }
        guard let _ = childVCs else {
            return
        }
        self.viewControllers = childVCs
        for (idx, tab) in tabModels!.enumerated() {
            let item = self.tabBar.items![idx]
            item.title = tab.title
            item.image = tab.image
            item.selectedImage = tab.selecedIMG
        }

    }
    
//    func childs() -> [UIViewController] {
//        let storyboard = UIStoryboard(name: "Main", bundle: nil)
//        let vc0 = storyboard.instantiateViewController(withIdentifier: "ViewController")
//        let vc1 = storyboard.instantiateViewController(withIdentifier: "SectionTwoVC")
//        return [NavigationC(rootViewController: vc0), NavigationC(rootViewController: vc1)]
//    }
//
//    func tabs() -> [TabModel] {
//        let tab0 = TabModel(title: "第一", image: UIImage(named: "tabbar0-normal")!.withRenderingMode(.alwaysOriginal), selecedIMG: UIImage(named: "tabbar0-selected")!.withRenderingMode(.alwaysOriginal))
//        let tab1 = TabModel(title: "第二", image: UIImage(named: "tabbar1-normal")!.withRenderingMode(.alwaysOriginal), selecedIMG: UIImage(named: "tabbar1-selected")!.withRenderingMode(.alwaysOriginal))
//        return [tab0, tab1]
//    }

}

extension TabbarC {
    
}
